package basic.study.leetcode;

/**
 * @author Kevin
 * @date 2020/3/1 21:45
 * 验证搜索二叉树
 */
public class Mid98 {
    public static void main(String[] args) {

    }

    public boolean isVaildBST(TreeNode root) {
        return helper(root, null, null);
    }

    //return helper(root, null, null)
    public boolean helper(TreeNode node, Integer lower, Integer upper) {
        if (node == null) return true;

        if (lower != null && node.val <= lower) return false;
        if (upper != null && node.val >= upper) return false;

        if (!helper(node.left, lower, node.val)) return false;
        if (!helper(node.right, node.val, upper)) return false;

        return true;
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
   }
}
